package com.chen.spzx.cloud.order.service.impl;

import com.chen.spzx.cloud.entity.Order;
import com.chen.spzx.cloud.entity.User;
import com.chen.spzx.cloud.feign.client.UserFeignClient;
import com.chen.spzx.cloud.order.mapper.OrderMapper;
import com.chen.spzx.cloud.order.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author cgj
 * @date 3/29/25 10:09 AM
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
  //  @Autowired
//    private RestTemplate restTemplate;
  //  @Autowired
  //  private DiscoveryClient discoveryClient;
    @Autowired
    private UserFeignClient userFeignClient;
    //使用feign 可以更简便的发请求, 代替restTemplate

    @Override
    public Order selectByOrderId(Long orderId) {
        // 根据orderId查询订单信息
        Order order = orderMapper.selectByOrderId(orderId);
        // 获取示例列表
//        List<ServiceInstance> instances = discoveryClient.getInstances("spzx-cloud-user");
//        ServiceInstance serviceInstance = instances.get(0);
//        String host = serviceInstance.getHost(); //ip
//        int port = serviceInstance.getPort(); //端口
        // 根据 order中的userId查询用户信息
//        User user = restTemplate.getForObject("http://127.0.0.1:10100/api/user/selectByUserId/" + order.getUserId(), User.class);
//        User user = restTemplate.getForObject("http://"+host+":"+port+"/api/user/selectByUserId/" + order.getUserId(), User.class);
//        User user = restTemplate.getForObject("http://spzx-cloud-user/api/user/selectByUserId/" + order.getUserId(), User.class);
    //    User user = restTemplate.getForObject("http://提供服务的名字(Service Name)/api/user/selectByUserId/" + order.getUserId(), User.class);
        User user = userFeignClient.selectById(order.getUserId());
        order.setUser(user);
        return order;
    }
}
